package cn.qyd.scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

/**
 * @author 邱运铎
 * @date 2024-03-24 18:32
 */
public class ScheduledThreadPoolDemo {
    private static final ScheduledThreadPoolExecutor scheduled = new ScheduledThreadPoolExecutor(1,
            Executors.defaultThreadFactory());

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        scheduled.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                if (haveMsgAtCurrentTime()) {
                    System.out.println(dateFormat.format(new Date()));
                    System.out.println("注意了，我要发送消息了");
                }
            }
        },1,1, TimeUnit.SECONDS);
    }

    public static boolean haveMsgAtCurrentTime() {
        //正常逻辑是从数据库或其他数据存放地方取出当前时间段需要执行的定时任务
        //这里直接返回true
        return true;
    }
}
